<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="false"
    >
      <!-- 标记 -->
      <template #statusFlag="{ record }">
        <template v-if="record.flag === 0">
          <Tag :color="'green'">启用</Tag>
        </template>
        <template v-if="record.flag === 5">
          <Tag :color="'yellow'">禁用</Tag>
        </template>
        <template v-if="record.flag === 9">
          <Tag :color="'red'">已删除</Tag>
        </template>
      </template>

      <!-- 是否在菜单中显示 -->
      <template #visibleFlag="{ record }">
        <template v-if="record.visible === true">
          <Tag :color="'cadetBlue'">是</Tag>
        </template>
        <template v-if="record.visible === false">
          <Tag :color="'DarkGray'">否</Tag>
        </template>
      </template>

      <!-- 文章是否需要审核 -->
      <template #auditFlag="{ record }">
        <template v-if="record.auditcount === 0">
          <Tag color="cyan">直接发布</Tag>
        </template>
        <template v-if="record.auditcount === 1">
          <Tag color="blue">二审发布</Tag>
        </template>
        <template v-if="record.auditcount === 2">
          <Tag color="magenta">三审三校</Tag>
        </template>
      </template>

      <!-- 是否控制栏目权限 -->
      <template #deptauthFlag="{ record }">
        <template v-if="record.deptauth === true">
          <Tag :color="'cadetBlue'">是</Tag>
        </template>
        <template v-if="record.deptauth === false">
          <Tag :color="'DarkGray'">否</Tag>
        </template>
      </template>

      <!-- 是否允许发文 -->
      <template #candraftFlag="{ record }">
        <template v-if="record.candraft === true">
          <Tag :color="'cadetBlue'">是</Tag>
        </template>
        <template v-if="record.candraft === false">
          <Tag :color="'DarkGray'">否</Tag>
        </template>
      </template>

      <template #toolbar>
        <a-button type="primary" @click="onAddCategory"> 新增 </a-button>
        <a-button type="primary" @click="onMoreDept"> 批量部门设置 </a-button>
      </template>

      <!-- 栏目名称没有子级栏目时，没有图标文字对不齐，设置缩进对齐 -->
      <template #name="{ record }">
        <template v-if="record.children === null">
          <span style="display: block; text-indent: 1.7em">{{ record.name }}</span>
        </template>
        <template v-else>
          <span>{{ record.name }}</span>
        </template>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditCategory.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteCategory.bind(null, record),
              },
            },
            {
              icon: 'ant-design:picture-outlined',
              label: '轮播图管理',
              color: 'success',
              onClick: openSwipperManagement.bind(null, record),
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '设置部门',
              color: 'warning',
              onClick: onSetDepts.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <addOrUpdate @register="registerAddOrUpdateForm" @success="afterSuccess" />
    <swipperTable @register="registerSwipperTble" @success="afterSuccess" />
    <deptTreeModel @register="registerDeptTree" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, provide } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';

  import { Tag } from 'ant-design-vue';

  import { deleteCategory, queryTreeTableDetail } from '/@/api/cms/category';
  import { categoryTableColumns } from './category.data';

  //引入子组件
  import addOrUpdate from './AddOrUpdate.vue';
  import swipperTable from './SwipperTable.vue';
  import deptTreeModel from './DeptTreeModel.vue';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';

  const [registerAddOrUpdateForm, { openModal: openAddOrUpdateForm }] = useModal();
  const [registerSwipperTble, { openModal: openSwipperTable }] = useModal();
  const [registerDeptTree, { openModal: openDeptTree }] = useModal();

  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'CategoryManagement',
    components: { BasicTable, TableAction, addOrUpdate, Tag, swipperTable, deptTreeModel },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('栏目管理');

      /**
       * 请求后台查询接口返回数据显示
       */
      const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys }] = useTable({
        title: '栏目管理',
        isTreeTable: true,
        api: queryTreeTableDetail, //api中指定的为table加载数据对应的查询方法
        searchInfo: { isall: true, isdept: false, isvisible: true }, //额外的请求参数
        columns: categoryTableColumns, //表头数据
        // formConfig: {
        //   labelWidth: 100, //设置查询表单中label的宽度
        //   schemas: categoryQueryConditionFormSchema, // form查询表单
        //   showResetButton: true, //查询条件部分中是否显示重置按钮
        // },
        // useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 300,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        pagination: false, //关闭分页
        striped: false,
        canResize: false,
        rowSelection: { type: 'checkbox' },
        rowKey: 'id',
        clickToRowSelect: false, //开启点击行选中/取消checkbox
      });

      /**
       * 打开新增栏目窗口
       */
      function onAddCategory() {
        //使用props传递数据至子组件，在data参数中指定
        openAddOrUpdateForm(true, { op: OperationTypeEnum.ADD });
      }

      /**
       * 打开编辑栏目窗口
       */
      function onEditCategory(record: Recordable) {
        openAddOrUpdateForm(true, { op: OperationTypeEnum.EDIT, dataId: record.id });
      }

      /**
       * 删除栏目信息
       */
      function onDeleteCategory(record: Recordable) {
        deleteCategory(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 打开批量设置部门窗口
       */
      function onMoreDept() {
        if (getSelectRowKeys().length == 0) {
          createMessage.warning('请勾选栏目');
        } else {
          openDeptTree(true, { op: '批量设置栏目关联部门', cids: getSelectRowKeys() });
        }
      }

      /**
       * 打开设置部门窗口
       */
      function onSetDepts(record: Recordable) {
        openDeptTree(true, { op: '设置栏目关联部门', dataId: record.id });
      }

      /**
       * 打开轮播图管理窗口
       */
      function openSwipperManagement(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateType: OperationTypeEnum.SET_RELATION,
          dataId: record.id,
          dataTitle: record.name,
        };
        openSwipperTable(true, propsData);
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
        clearSelectedRowKeys();
      }

      provide('moduleName', moduleName);

      return {
        moduleName,
        registerTable,
        afterSuccess,
        registerAddOrUpdateForm,
        registerDeptTree,
        registerSwipperTble,
        onAddCategory,
        onEditCategory,
        onDeleteCategory,
        onSetDepts,
        onMoreDept,
        openSwipperManagement,
      };
    },
  });
</script>
